//
//  CSPromiseTests.m
//  cspsdk
//
//  Created by luosong on 15/8/31.
//  Copyright (c) 2015 CHANJET. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import "CSPIdentityService.h"
#import "CSPTask.h"
#import "CSPromise.h"
#import "CSPCategory.h"
#import "CSPLogging.h"

@interface CSPromiseTests : XCTestCase

@end

@interface CSPromiseTests ()
- (CSPMKPromise *)users;
@end

@implementation CSPromiseTests

- (void)setUp {
    [super setUp];
    [CSPLogger defaultLogger].logLevel = CSPLogLevelVerbose;
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

- (void)testAnyPromise {
    id ex1 = [self expectationWithDescription:@""];

//    CSPMKPromise *promise = [CSPMKPromise promiseWithValue:@"Hello"];
//    promise.then(^(NSString * string) {
//        XCTAssertNotNil(string);
//        CSPLogInfo(@"%@", string);
//        [ex1 fulfill];
//    });

    dispatch_csp_promise(^ {
        return nil;
    }).then(^ {
        CSPLogInfo(@"%@", @"asdfasf");
        [ex1 fulfill];
    });

//    [self users].then(^(id result) {
//        XCTAssertNotNil(result);
//        [ex1 fulfill];
//    }).catch(^(NSError *error) {
//        CSPLogError(@"%@", error.description);
//    });

    [self waitForExpectationsWithTimeout:10 handler:nil];
}

- (CSPMKPromise *)users {
    NSLog(@"Get PMKPromise");
    return [CSPMKPromise promiseWithResolver:^(CSPMKResolver resolve) {
        CSPIdentityService *service = [[CSPIdentityService alloc] init];
        CSPIdentityLoginWithAccountRequest *request = [[CSPIdentityLoginWithAccountRequest alloc] init];
        request.account = @"17001098622";
        request.password = @"2238301".csp_md5String;
        request.needInviteList = @"0";
        request.coop = @"iOS";
        request.endpoint_id = [NSString stringWithFormat:@"%f", [NSDate date].timeIntervalSince1970];

        [[service loginWithAccountPassword:request] continueWithBlock:^id(CSPTask *task) {
//            XCTAssertNil(task.error);
//            XCTAssertNil(task.exception);
//            XCTAssertNil(task.result);
            resolve(task.error ?: task.result);
            return nil;
        }];
    }];
}


@end
